<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Identity extends Modules_Controller
{

	function __construct()
	{
		parent::__construct();

		$this->rules = array(
			"rule" => array(
				array(
					"field" => "title",
					"label" => lang('title'),
					"rules" => "trim|required|min_length[1]"
				)
				,array(
					"field" => "timeline",
					"label" => lang('time'),
					"rules" => "trim|strtotime"
				)
				,array(
					"field" => "content",
					"label" => lang('conent'),
					"rules" => "trim"
					// link_create tag 生成
				)
				,array(
					"field" => "photo",
					"label" => lang('photo'),
					"rules" => "trim"
				)
			)
		);

	}
	public function index($cid=false,$page=1)
	{
		// 栏目路径
		$vdata['cpath']= $this->mcol->get_path_more($this->cid);
		$vdata['cchildren'] = $this->mcol->get_cols($this->cid);
		$title = $this->mcol->get_one($this->cid,"title");
		$vdata['title'] = $title['title'];

		$limit = $this->page_limit;
		$this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

		$order = $this->_index_orders();
		if ($this->input->get('order',TRUE)) {
			// TODO: order
			// $orders = explode("-",$this->input->get('order',TRUE));
			$order = $this->input->get('order',TRUE);
		}
		$where_in = array("identity.cid"=>$this->cid,"identity.ccid"=>$this->ccid);
		// 条件必须

		$where = array_merge($this->_index_where(),$where_in);
		$count = $this->model->get_count_all($where,false,array('account','account.id=identity.uid','left'));
		$vdata['pages'] = $this->_pages_join(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$count,4);
		$fields = 'account.uname as username,account.phone,account.ctype as actype,identity.*';
		//var_dump("132123");die();
		$vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where,$fields,false,array('account','account.id=identity.uid','left'));
		$total = $this->model->get_status_nums();
		$vdata['total'] = $total['total'];
		$this->_display($vdata);
	}
	// 搜索
	public function search($cid=false,$page=1){
		$vdata['cpath']= $this->mcol->get_path_more($this->cid);
		$vdata['cchildren'] = $this->mcol->get_cols($this->cid);
		$title = $this->mcol->get_one($this->cid,"title");
		$vdata['title'] = $title['title'];
		$limit = $this->page_limit;
		$this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
		$order = $this->input->get('order',TRUE) ? $this->input->get('order',TRUE):FALSE;
		$where = array();
		$where['identity.cid'] = $this->cid;
		if ($ctype = $this->input->get('ctype',TRUE) AND is_numeric($ctype)) {
			$ctypes_ids = $this->mctypes->find_ids($this->cid,$ctype);
			$where['in'] = array('identity.ctype',$ctypes_ids);
		}
		if ($wh = $this->_search_where()) {
			$where = array_merge($where,$wh);
		}
		if($this->input->get('q',TRUE)){
			$where_q = array(
				'like title'=> array('title',$this->input->get('q',TRUE))
			);
			$where = array_merge($where,$where_q);
		}
		$vdata['pages'] = $this->_pages(site_url($this->class.'/search/'.$this->cid.'/'),$limit,$where,4);
		$fields = 'account.uname as username,account.phone,identity.*';

		$vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where,$fields,false,array('account','account.id=identity.uid','left'));
		$this->_display($vdata,$this->class.'_index.php');
	}
	// 为search提供where条件数组
	protected function _search_where(){
		return false;
	}
	// 此处返回数组
	protected function _index_orders(){
		return array( 'identity.sort_id'=>'desc' );
	}
	protected function _index_where(){
		$arr =array();
		if (isset($_GET['ctype']) && $_GET['ctype']) {

			$arr['identity.ctype'] = $_GET['ctype'];
		}
		if (isset($_GET['uname']) && $_GET['uname']) {
			$arr['account.uname'] = $_GET['uname'];
		}
		if (isset($_GET['status'])) {
			if($_GET['status'] != 3) {
				$arr['identity.status'] = $_GET['status'];
			}

		}
		if (isset($_GET['old_com_name']) && $_GET['old_com_name']) {
			$arr['like 1'] = array('account.old_com_name',$_GET['old_com_name']);
			//$arr['account.old_com_name'] = $_GET['old_com_name'];
		}
		if (isset($_GET['com_name']) && $_GET['com_name']) {
			$arr['like 2'] = array('account.com_name',$_GET['com_name']);
			//$arr['account.com_name'] = $_GET['com_name'];
		}
		//var_dump($arr);die();
		return $arr;
	}
	public function status() {
		$key = '';
		if ($this->input->post('ids') ) {
			$key = explode(',',$this->input->post('ids'));
		}else{
			$vdata = array('status'=>0,'msg'=>lang('modules_no_id'));
		}

		$where = FALSE;

		if ($this->input->get('c')) {
			$where = array('cid' => $this->input->get('c'));
		}else{
			$vdata = array('status'=>0,'msg'=>lang('modules_no_col_id'));
		}

		$audit = $this->input->post('status');

		$msg = array(lang('modules_status_loading'),lang('modules_status_false'),lang('modules_status_true'));

		if (!isset($vdata['status'])) {
			foreach($key as $id) {
				$res = $this->model->set($id,array('status'=>$audit));
			}
			if ($res) {
				$vdata = array('status'=>1,'msg'=>$msg[$audit]);
				$this->_status_after($key,$audit);
				if (is_array($key)) {
					$this->mlogs->add('status',lang('modules_status_id').$this->input->post('ids').lang('modules_status_for').$audit);
				}else{
					$this->mlogs->add('status',lang('modules_status_id').$key.lang('modules_status_for').$audit);
				}
			}else{
				$vdata = array('status'=>0,'msg'=>lang('modules_status_err_select'));
			}
		}

		if($this->input->is_ajax_request()){
			$this->output->set_content_type('application/json')->set_output(json_encode($vdata));
		}else{
			$this->load->view('msg',$vdata);
		}
	}
	public function _status_after($key,$status) {
		$this->load->model('company_model','mcompany');
		$this->load->model('account_info_model','maccount_info');
		$ids = explode(',',$key);
		if($status == 2) {
			//审核通过
			foreach($ids as $id) {
				$ident = $this->model->get_one($id);
				//$company_one = $this->mcompany->get_one(array('name'=>$ident['com_name'],'short'=>$ident['com_short']));
				$company_one = $this->mcompany->get_one(array('name'=>$ident['com_name'],'or'=>array('short'=>$ident['com_short'])));
				$account = $this->macc->get_one($ident['uid'],'id,company,ctype,com_name,com_short,phone');
				//发短信
				$this->load->model('smscode_model', 'msmscode');
				$this->msmscode->sendCode($account['phone'], 4,4);

				if($ident['ctype'] == 33) {//公司名称变更
					if($account['ctype'] == 1) {
						if($company_one) {
							$company_id = $company_one['id'];
							//$this->mcompany->update(array('name'=>$ident['com_name'],'short'=>$ident['com_short']),array('id'=>$account['company']));
						} else {
							$company_id = $account['company'];
							//$this->mcompany->create(array('name'=>$ident['com_name'],'short'=>$ident['com_short'],'uid'=>$account['id']));
							$this->mcompany->update(array('name'=>$ident['com_name'],'short'=>$ident['com_short'],'initials'=>pinyin($ident['com_short'],'UTF8',true)),array('id'=>$account['company']));
						}
						$this->macc->update(array('company'=>$company_id),array('id'=>$account['id']));
					} else {
						$this->macc->update(array('com_name'=>$ident['com_name'],'com_short'=>$ident['com_short']),array('id'=>$account['id']));
					}

				} elseif($ident['ctype'] == 34) {
					//工作单位变更
					if($account['ctype'] == 1) {
						if(!$company_one) {
							$company = $this->mcompany->create(array('name'=>$ident['com_name'],'short'=>$ident['com_short'],'uid'=>$account['id'],'initials'=>pinyin($ident['com_short'],'UTF8',true)));
						} else {
							$company = $company_one['id'];
						}
						$this->macc->update(array('company'=>$company,'ctype'=>$ident['actype'],'position'=>$ident['position']),array('id'=>$account['id']));
					} else {
						$this->macc->update(array('com_name'=>$ident['com_name'],'com_short'=>$ident['com_short'],'ctype'=>$ident['actype'],'position'=>$ident['position']),array('id'=>$account['id']));
					}
					switch($account['ctype']) {
						case 1:
							$data = array('industry'=>'','tags'=>'');
							break;
						case 2:
							$data = array('interested'=>'','fund_stage'=>'','single_project'=>'');
							break;
						case 3:
							$this->macc->update(array('board'=>''),array('id'=>$account['id']));
							$data = array('industry'=>'','interested'=>'');
							break;
						case 4:
							$data = array('good_industry'=>'','interested'=>'');
							break;
						case 5:
							$data = array('interested'=>'');
							break;
						case 6:
							$data = array('interested'=>'');
							break;
					}
					$this->maccount_info->update($data,array('uid'=>$account['id']));
				}
				$this->msmscode->sendCode($account['phone'], 4,4);
				//getui_push($ident['uid'],2,$ident['uid'],'尊敬的用户：您的变更申请已通过审核，账户可正常使用。');

			}
		} else {
			//审核不通过
			foreach($ids as $id) {
				$ident = $this->model->get_one($id);
				$account = $this->macc->get_one($ident['uid'],'phone');
				//发短信
				$this->load->model('smscode_model', 'msmscode');
				$this->msmscode->sendCode($account['phone'], 5,5);
				//getui_push($ident['uid'],2,$ident['uid'],'尊敬的用户： 您的变更申请未通过审核，请重新提交申请。');

			}
		}
	}
	public function one() {
		$data = $this->input->get();
		$res = $this->model->set($data['id'],array('status'=>$data['status']));
		if($res) {
			$this->_status_after($data['id'],$data['status']);
			redirect(base64_decode($data['ref']));
		}
	}
	public function copypro()
    {
        $ids = $this->input->post('ids');

        $rs=$this->model->get_one($ids);

        unset($rs['id']);
        unset($rs['sort_id']);
        unset($rs['timeline']);

        $id = $this->model->create($rs);
        if ($id) {
            $vdata['msg'] = '复制成功，请刷新查看';
            $vdata['status'] = 1;
        }else{
            $vdata['msg'] = '复制失败，请刷新后重试';
            $vdata['status'] = 0;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }

}
